package com.zhong.gang.applib.base.act;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhong.gang.applib.R;

import java.util.List;

public abstract class ActBaseRefresh<T extends ViewDataBinding,M extends BaseQuickAdapter>
        extends ActBase<T> implements OnRefreshListener, OnLoadMoreListener {
    public int page = 1;
    public M adapter;
    public SmartRefreshLayout swipeRefreshLayout;
    private boolean isAddMore = true;
    private boolean isEmtry = true;
    public void setAddMore(boolean isAddMore){
        this.isAddMore = isAddMore;
    }

    public void setEmtry(boolean isEmtry){
        this.isEmtry = isEmtry;
    }


    @Override
    public boolean showLoading() {
        return true;
    }

    public void initrefrsh(SmartRefreshLayout swipeRefreshLayout, M adapter, RecyclerView recyclerView){
        initrefrsh(swipeRefreshLayout,adapter,recyclerView, R.mipmap.icon_not_data,"暂无数据~~");
    }

    public void initrefrsh(SmartRefreshLayout swipeRefreshLayout, M adapter, RecyclerView recyclerView, boolean isFill){
        initrefrsh(swipeRefreshLayout,adapter,recyclerView, R.mipmap.icon_not_data,"暂无数据~~",isFill);
    }

    public void initrefrsh(SmartRefreshLayout swipeRefreshLayout
            , M adapter, RecyclerView recyclerView, @DrawableRes int drawableRes, String str){
        initrefrsh(swipeRefreshLayout,adapter,recyclerView,drawableRes,str,true);
    }

    public void initrefrsh(SmartRefreshLayout swipeRefreshLayout
            , M adapter, RecyclerView recyclerView, @DrawableRes int drawableRes, String str, boolean isFill){
        this.adapter = adapter;
        this.swipeRefreshLayout = swipeRefreshLayout;
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setEnableAutoLoadMore(false);
            swipeRefreshLayout.setOnRefreshListener(this);
            swipeRefreshLayout.setEnableLoadMore(isAddMore);
            if (isAddMore)
                swipeRefreshLayout.setOnLoadMoreListener(this);
        }
        if (adapter != null) {
            if (isEmtry) {
                addEmtryView(adapter,drawableRes,str,isFill);
            }
            if (recyclerView != null)
                recyclerView.setAdapter(adapter);
            adapter.setHeaderFooterEmpty(true,true);
        }
    }


    public void addEmtryView(BaseQuickAdapter adapter, int drawableRes, String str, boolean isFill){
        View view = LayoutInflater.from(this).inflate(R.layout.layout_emtry_view, null);
        ImageView icon_not_data = view.findViewById(R.id.icon_not_data);
        TextView title = view.findViewById(R.id.title);
        icon_not_data.setImageResource(drawableRes);
        title.setText(str);
        adapter.setEmptyView(view);
        adapter.getEmptyView().setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , isFill ?ViewGroup.LayoutParams.FILL_PARENT:ViewGroup.LayoutParams.WRAP_CONTENT));

    }


    @Override
    public void onRefresh(RefreshLayout layout) {
        page = 1;
        getData();
    }

    public void onRefresh(){
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.resetNoMoreData();
        }
        onRefresh(null);
    }


    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        getData();
    }

    @Override
    public void getData() {
        super.getData();
        if (swipeRefreshLayout != null && page == 1) {
            swipeRefreshLayout.resetNoMoreData();
        }
    }

    public void cloaseRefresh(){
        if (swipeRefreshLayout != null){
            swipeRefreshLayout.finishLoadMore(true);
            swipeRefreshLayout.finishRefresh(true);
        }
    }
    public void setData(List<?> data){
        if (data != null) {
            if (page == 1)
                adapter.setNewData(data);
            else
                adapter.addData(data);
        }
        cloaseRefresh();
        if (swipeRefreshLayout != null){
            if (data.size() <=0) {
                swipeRefreshLayout.finishLoadMoreWithNoMoreData();
            }else {
                swipeRefreshLayout.finishLoadMore();
            }
        }

    }

    @Override
    public void showMainFragemt() {
        super.showMainFragemt();
        cloaseRefresh();
    }

    @Override
    public void showErrorFragment() {
        super.showErrorFragment();
        cloaseRefresh();
    }

    @Override
    public void showLoadingFrgamnet() {
        super.showLoadingFrgamnet();
        cloaseRefresh();
    }
}
